//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "server\fn_serverSetupConquer.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object"];
DEFAULT_PARAM(_object,TMF_REGISTER_OBJECT,objNull);

if (!isnull _object) then {
	private ["_action","_parameters","_i","_conquer_msg","_conquer_zone","_conquer_owner","_shared_variable","_shared_variable_value","_conquer_code","_uid","_conquer_zone_center","_conquer_zone_size"
			,"_conquer_zone_angle","_conquer_zone_rect","_object_identifier","_conquer_left","_base_conquer_timeout","_conquer_timeout"];
	DEFAULT_PARAM(_object,TMF_REGISTER_OBJECT,objNull);
	DEFAULT_PARAM(_action,TMF_REGISTER_ACTION,[]);
	DEFAULT_PARAM(_instance_id,TMF_REGISTER_INSTANCE,TMF_SERVER_INSTANCE_ID);
	_action_id = [_action] call FFUNC(actionGetID);
	_base_conquer_timeout = ["BASE_CONQUERED_TIMEOUT",15] call FFUNC(getFrameworkValue);
	TRACE_1("Action",_action);
	_parameters = [_action] call FFUNC(actionGetParams);
	_conquer_msg = [_parameters, "CONQUER_MSG", "",THIS_FILE] call FFUNC(hashGet);
	_conquer_zone = [_parameters, "CONQUER_ZONE", [getPos _object],THIS_FILE] call FFUNC(hashGet);
	_conquer_owner = [_parameters, "CONQUER_OWNER", GVAR(side_enemy),THIS_FILE] call FFUNC(hashGet);
	_shared_variable = [_parameters, "SHARED_VAR", "",THIS_FILE] call FFUNC(hashGet);
	_conquer_left = [_parameters, "CONQUER_LEFT", 3,THIS_FILE] call FFUNC(hashGet);
	_conquer_code = [_parameters, "CONQUER_CODE", "",THIS_FILE] call FFUNC(hashGet);
	_conquer_timeout = [_parameters, "CONQUER_TIMEOUT", _base_conquer_timeout,THIS_FILE] call FFUNC(hashGet);
	_conquer_taker = [_parameters, "CONQUER_TAKER", GVAR(side_own),THIS_FILE] call FFUNC(hashGet);
	_shared_variable = _shared_variable call FFUNC(getStringVar);

	if (IS_ARRAY(_conquer_owner)) then {
		for "_i" from 0 to ((count _conquer_owner) - 1) step 1 do {
			if (IS_STRING((_conquer_owner select _i))) then {
				_conquer_owner set [_i, (switch {toupper((_conquer_owner select _i))} do { case "EAST": {east}; case "WEST": {west}; case "GUER": {resistance};	case "CIV": {civilian};	default{ GVAR(side_enemy)};	})];
			};
		};
	} else {
		if (IS_STRING(_conquer_owner)) then {
			_conquer_owner = switch {toupper(_conquer_owner)} do { case "EAST": {east}; case "WEST": {west}; case "GUER": {resistance};	case "CIV": {civilian};	default{ GVAR(side_enemy)};	};
		};
		_conquer_owner = [_conquer_owner];
	};
	if (IS_ARRAY(_conquer_taker)) then {
		for "_i" from 0 to ((count _conquer_taker) - 1) step 1 do {
			if (IS_STRING((_conquer_taker select _i))) then {
				_conquer_taker set [_i, (switch {toupper((_conquer_taker select _i))} do { case "EAST": {east}; case "WEST": {west}; case "GUER": {resistance};	case "CIV": {civilian};	default{ GVAR(side_own)};	})];
			};
		};
	} else {
		if (IS_STRING(_conquer_taker)) then {
			_conquer_taker = switch {toupper(_conquer_taker)} do { case "EAST": {east}; case "WEST": {west}; case "GUER": {resistance};	case "CIV": {civilian};	default{ GVAR(side_own)};	};
		};
		_conquer_taker = [_conquer_taker];
	};
	
	if (IS_STRING(_conquer_zone)) then {
		_conquer_zone = [getMarkerPos _conquer_zone, getMarkerSize _conquer_zone, MarkerDir _conquer_zone, ((MarkerShape _conquer_zone) == "RECTANGLE")];
	};
	_conquer_zone_center = GETVAL(_conquer_zone,0,getPos _object);
	_conquer_zone_size = GETVAL(_conquer_zone,1,100);
	_conquer_zone_angle = GETVAL(_conquer_zone,2,0);
	_conquer_zone_rect = GETVAL(_conquer_zone,3,true);
	
	if (IS_ARRAY(_conquer_zone_size)) then {
		_conquer_zone_size = GETVAL(_conquer_zone_size,0,100);
	};
	if (!isNil "_shared_variable") then {
		if (_shared_variable != "") then {
			[_instance_id,_shared_variable,false] spawn FFUNC(setMissionJIPVar);
			[QGVAR(all_var), [_instance_id,_shared_variable,false],THIS_FILE] call FFUNC(callEvent);
		};
	};
	_uid = ["UID"] call mso_fnc_fsmloop;
	_object setVariable [format["%1_%2",QGVAR(Conquer_Count),_action_id],0];
	_object setVariable [format["%1_%2",QGVAR(Conquer_Timeout),_action_id],_conquer_timeout];
	_object setVariable [format["%1_%2",QGVAR(Conquer_Start),_action_id],time];
	_object setVariable [format["%1_%2",QGVAR(Conquer_Done),_action_id],false];
	["itemAdd", [_uid, QUOTE(FFUNC(serverSetupConquerExec)),[_object,_action_id,_instance_id,[_conquer_zone_center,_conquer_zone_size,_conquer_zone_angle,_conquer_zone_rect,_conquer_left],_conquer_owner,_conquer_taker],4,"seconds"]] call mso_fnc_fsmloop;
};
TRACE("End");
TMFPROFILERSTOP;